package cn.itcast.anli.service.impl;

import cn.itcast.anli.dao.EmployeeDAO;
import cn.itcast.anli.dao.impl.EmployeeDAOImpl;
import cn.itcast.anli.domain.Employee;
import cn.itcast.anli.domain.PageBean;
import cn.itcast.anli.service.EmployeeService;

import java.util.List;
import java.util.Map;

public class EmployeeServiceImpl implements EmployeeService {
    EmployeeDAO dao = new EmployeeDAOImpl();


    @Override
    public void add(Employee employee) {
        dao.add(employee);
    }

    @Override
    public void delete(int id) {
        dao.delete(id);
    }

    @Override
    public Employee findById(int id) {
        Employee byId = dao.findById(id);
        return byId;
    }

    @Override
    public void modify(Employee employee) {
        dao.modify(employee);
    }

    @Override
    public void delSelected(String[] cbs) {
        //遍历集合
        for (String cb : cbs) {
            //删除指定id
            dao.delete(Integer.parseInt(cb));
        }
    }

    @Override
    public PageBean findEmpByPage(String _currentPage, String _row, Map<String,String[]> map) {
        int currentPage = Integer.parseInt(_currentPage);
        int row = Integer.parseInt(_row);

        //创建PageBean对象
        PageBean pageBean = new PageBean();
        //获取总记录数
        int totalCount = dao.totalCount(map);
        pageBean.setTotalCount(totalCount);
        //获取总页数
        int totalPage = totalCount % row == 0 ? totalCount / row : totalCount / row + 1;
        pageBean.setTotalPage(totalPage);
        //先做currentPage的值的判断
        if (currentPage>totalPage){
            currentPage = totalPage;
        }
        if (currentPage<1){
            currentPage = 1;
        }
        pageBean.setCurrentPage(currentPage);
        pageBean.setRow(row);
        //获取当页的Emp的List
        List<Employee> employees = dao.pageList(currentPage, row,map);
        pageBean.setList(employees);
        return pageBean;
    }
}
